<template>
  <div class="sidebar" :class="{ 'sidebar-collapsed': layoutStore.sidebarCollapsed }">
    <!-- 侧边栏头部 -->
    <div class="sidebar-header">
      <div class="logo">
        <div class="logo-icon">
          <span class="logo-letter">E</span>
        </div>
        <h1 v-if="!layoutStore.sidebarCollapsed" class="logo-text">EverWin Admin</h1>
      </div>
      
    </div>
    
    
    <!-- 侧边栏菜单 -->
    <div class="sidebar-menu">
      <div class="menu-container">
        <div 
          v-for="menu in menuStore.menus" 
          :key="menu.id"
          class="menu-item"
          :class="{ 
            'active': isMenuActive(menu),
            'collapsed': layoutStore.sidebarCollapsed
          }"
        >
          <!-- 有子菜单的情况 -->
          <div v-if="menu.children && menu.children.length > 0" class="menu-group">
            <div 
              class="menu-group-header"
              @click="toggleSubMenu(menu.id)"
            >
              <i :class="getMenuIcon(menu)" class="menu-icon"></i>
              <span v-if="!layoutStore.sidebarCollapsed" class="menu-title">{{ menu.meta?.title || menu.name }}</span>
              <i 
                v-if="!layoutStore.sidebarCollapsed"
                class="menu-arrow"
                :class="{ 'expanded': expandedMenus.includes(menu.id) }"
              ></i>
            </div>
            
            <!-- 子菜单 -->
            <div 
              v-if="!layoutStore.sidebarCollapsed && expandedMenus.includes(menu.id)"
              class="submenu"
            >
              <div 
                v-for="submenu in menu.children"
                :key="submenu.id"
                class="submenu-item"
                :class="{ 'active': activeMenuId === submenu.id }"
                @click="navigateToMenu(submenu)"
              >
                <span class="submenu-title">{{ submenu.meta?.title || submenu.name }}</span>
              </div>
            </div>
          </div>
          
          <!-- 没有子菜单的情况 -->
          <div 
            v-else
            class="menu-single"
            :class="{ 'active': activeMenuId === menu.id }"
            @click="navigateToMenu(menu)"
          >
            <i :class="getMenuIcon(menu)" class="menu-icon"></i>
            <span v-if="!layoutStore.sidebarCollapsed" class="menu-title">{{ menu.meta?.title || menu.name }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useLayoutStore } from '../../store/modules/layout'
import { useMenuStore } from '../../store/modules/menu'

// 初始化 store
const router = useRouter()
const route = useRoute()
const layoutStore = useLayoutStore()
const menuStore = useMenuStore()

// 解构 store 属性

// 展开的菜单列表
const expandedMenus = ref<string[]>(['system'])

// 当前激活的菜单ID
const activeMenuId = computed(() => {
  return menuStore.activeMenu
})

// 检查菜单是否激活（包括子菜单激活时父菜单也激活）
const isMenuActive = (menu: any) => {
  if (activeMenuId.value === menu.id) {
    return true
  }
  
  // 如果有子菜单，检查是否有子菜单被激活
  if (menu.children && menu.children.length > 0) {
    return menu.children.some((child: any) => activeMenuId.value === child.id)
  }
  
  return false
}

// 切换子菜单展开状态
const toggleSubMenu = (menuId: string) => {
  const index = expandedMenus.value.indexOf(menuId)
  if (index > -1) {
    expandedMenus.value.splice(index, 1)
  } else {
    expandedMenus.value.push(menuId)
  }
}

// 导航到菜单
const navigateToMenu = (menu: any) => {
  if (menu.path) {
    router.push(menu.path)
    menuStore.updateActiveMenu(menu.path)
  }
}

// 获取菜单图标
const getMenuIcon = (menu: any) => {
  const iconMap: Record<string, string> = {
    'dashboard': 'pi pi-home',
    'system': 'pi pi-cog',
    'user-list': 'pi pi-users',
    'dept-list': 'pi pi-building',
    'role-list': 'pi pi-key',
    'post-list': 'pi pi-briefcase',
    'menu-list': 'pi pi-list',
    'log-list': 'pi pi-file',
    'org-list': 'pi pi-sitemap',
    'settings': 'pi pi-cog',
    'profile': 'pi pi-user',
    'route-test': 'pi pi-test-tube'
  }
  
  return iconMap[menu.id] || menu.icon || 'pi pi-circle'
}


// 监听路由变化，更新激活菜单
watch(() => route.path, (newPath) => {
  menuStore.updateActiveMenu(newPath)
}, { immediate: true })
</script>

<style scoped>
.sidebar {
  width: 240px;
  height: 100%;
  background-color: #001529;
  transition: width 0.3s ease;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.sidebar.sidebar-collapsed {
  width: 64px;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid #002140;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 64px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-icon {
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, #1890ff, #722ed1);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.3);
}

.logo-letter {
  color: white;
  font-size: 18px;
  font-weight: bold;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.logo-text {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
}




.sidebar-menu {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.menu-container {
  padding: 0 8px;
}

.menu-item {
  margin-bottom: 4px;
}

.menu-group-header,
.menu-single {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  color: rgba(255, 255, 255, 0.65);
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.3s;
  position: relative;
}

.menu-group-header:hover,
.menu-single:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.menu-group-header.active,
.menu-single.active {
  background-color: #34D3fB;
  color: #fff;
  border-radius: 3px;
}

.menu-group-header.active::before,
.menu-single.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background-color: #fff;
}

.menu-icon {
  font-size: 16px;
  margin-right: 12px;
  width: 16px;
  text-align: center;
}

.menu-title {
  flex: 1;
  font-size: 14px;
  white-space: nowrap;
}

.menu-arrow {
  font-size: 12px;
  transition: transform 0.3s;
}

.menu-arrow.expanded {
  transform: rotate(90deg);
}

.submenu {
  background-color: rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  margin: 4px 0;
  overflow: hidden;
}

.submenu-item {
  padding: 8px 16px 8px 40px;
  color: rgba(255, 255, 255, 0.65);
  cursor: pointer;
  transition: all 0.3s;
  font-size: 13px;
  position: relative;
}

.submenu-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.submenu-item.active {
  background-color: #34D3fB;
  color: #fff;
  border-radius: 3px;
}

.submenu-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background-color: #fff;
}

/* 折叠状态下的样式 */
.sidebar-collapsed .sidebar-header {
  align-items: center;
  justify-content: center;
  padding: 16px 8px;
}

.sidebar-collapsed .logo {
  justify-content: center;
}

.sidebar-collapsed .logo-text {
  display: none;
}



.sidebar-collapsed .menu-group-header,
.sidebar-collapsed .menu-single {
  justify-content: center;
  padding: 12px 8px;
}

.sidebar-collapsed .menu-icon {
  margin-right: 0;
}

.sidebar-collapsed .menu-title,
.sidebar-collapsed .menu-arrow {
  display: none;
}

.sidebar-collapsed .submenu {
  display: none;
}

/* 滚动条样式 */
.sidebar-menu::-webkit-scrollbar {
  width: 4px;
}

.sidebar-menu::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar-menu::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
}

.sidebar-menu::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    transform: translateX(-100%);
  }
  
  .sidebar:not(.sidebar-collapsed) {
    transform: translateX(0);
  }
}
</style>